*------------------------------------------------------------------------------*
*																			   *
*							TV-Content 2009								   	   *
*																			   *
*------------------------------------------------------------------------------*

* Project: "Voting in Context" Book
* Title: "Always late? Stability and change in individuals‘ time of vote decisions"
* Author: Maria Preißinger


use ZA5306_v1-1-0.dta, clear

drop if filterbei==0			//at the article-level
drop if B10==.					//topically not classifiable: greetings etc.

codebook B10
sum if ((B10==1 | B10==2) | (B11==1 | B11==2))  //only german federal articles 
tab B10 B05, mis	// 100% of topically not classifiable (B05=2) are missings in B10
tab B10 B11, mis	


*---------------------------------------------
* Date
*---------------------------------------------

gen date = mdy(V04, V05, V03)  //in Stata-Date format
format date %d  //make it readable


/*time of survey: June, 23 till Sept, 21 --> includes all but Wave 7 of the campaign panel*/

*------------------------------------------
* Evaluation of the articles
*------------------------------------------

tab1 B22a-B22o, m
recode B22a-B22l (0=.) (1=1) (2=0.5) (3 9=0) (4=-0.5) (5=-1) (.=.)

tab B22a  //Attention: value label are not correct anymore
lab val B22a-B22l  //delete value labels
tab B22a

 *CDU
egen toncdurtl = rowmean(B22a-B22d) if ((B10==1 | B10==2) | (B11==1 | B11==2)) &  V02=="RTL aktuell"
egen toncdusat = rowmean(B22a-B22d) if ((B10==1 | B10==2) | (B11==1 | B11==2)) &  V02=="Sat.1 Nachrichten"
egen toncduard = rowmean(B22a-B22d) if ((B10==1 | B10==2) | (B11==1 | B11==2)) &  V02=="Tagesschau"
egen toncduzdf = rowmean(B22a-B22d) if ((B10==1 | B10==2) | (B11==1 | B11==2)) &  V02=="heute"
														
	
*SPD
egen tonspdrtl = rowmean(B22e B22f) if ((B10==1 | B10==2) | (B11==1 | B11==2)) &  V02=="RTL aktuell"
egen tonspdsat = rowmean(B22e B22f) if ((B10==1 | B10==2) | (B11==1 | B11==2)) &  V02=="Sat.1 Nachrichten"
egen tonspdard = rowmean(B22e B22f) if ((B10==1 | B10==2) | (B11==1 | B11==2)) &  V02=="Tagesschau"
egen tonspdzdf = rowmean(B22e B22f) if ((B10==1 | B10==2) | (B11==1 | B11==2)) &  V02=="heute"

*FDP
egen tonfdprtl = rowmean(B22g B22h) if ((B10==1 | B10==2) | (B11==1 | B11==2)) &  V02=="RTL aktuell"
egen tonfdpsat = rowmean(B22g B22h) if ((B10==1 | B10==2) | (B11==1 | B11==2)) &  V02=="Sat.1 Nachrichten"
egen tonfdpard = rowmean(B22g B22h) if ((B10==1 | B10==2) | (B11==1 | B11==2)) &  V02=="Tagesschau"
egen tonfdpzdf = rowmean(B22g B22h) if ((B10==1 | B10==2) | (B11==1 | B11==2)) &  V02=="heute"

	
*The Greens
egen tongrurtl = rowmean(B22i B22j) if ((B10==1 | B10==2) | (B11==1 | B11==2)) &  V02=="RTL aktuell"
egen tongrusat = rowmean(B22i B22j) if ((B10==1 | B10==2) | (B11==1 | B11==2)) &  V02=="Sat.1 Nachrichten"
egen tongruard = rowmean(B22i B22j) if ((B10==1 | B10==2) | (B11==1 | B11==2)) &  V02=="Tagesschau"
egen tongruzdf = rowmean(B22i B22j) if ((B10==1 | B10==2) | (B11==1 | B11==2)) &  V02=="heute"

*The Left
egen tonlinkrtl = rowmean(B22k B22l) if ((B10==1 | B10==2) | (B11==1 | B11==2)) &  V02=="RTL aktuell"
egen tonlinksat = rowmean(B22k B22l) if ((B10==1 | B10==2) | (B11==1 | B11==2)) &  V02=="Sat.1 Nachrichten"
egen tonlinkard = rowmean(B22k B22l) if ((B10==1 | B10==2) | (B11==1 | B11==2)) &  V02=="Tagesschau"
egen tonlinkzdf = rowmean(B22k B22l) if ((B10==1 | B10==2) | (B11==1 | B11==2)) &  V02=="heute"


sum ton*

*----------------------------------------
* aggregate for each day
*----------------------------------------

collapse (mean) ton*, by(date)

//browse

expand 2 if date==date("26sep2009", "DMY"), gen(expanded)

/* survey of the TV-News ends before the campaing panel does. As we want to assign the 
average tone of conversation of a week before the the interview, we can add one day.
Solution: 
Copy the last observation and append it below. That works with the "expand"-command.
Set values of all variables for the entire additional day to "missing". */


gen n = _n			//create serial number
order n				//right in the beginning of the dataset
					
replace date = date+1 if n==92		//adjust dates for the new observations


foreach var of varlist toncdurtl-tonlinkzdf {
replace `var' = . if n>=92
}



*-----------------------------------------------
* create lags for up to 7 days before
*------------------------------------------------


foreach s in ard zdf rtl sat {
foreach num of numlist 1/7 {
foreach partei in cdu spd fdp gru link {
	gen lg`num'ton`partei'`s' = ton`partei'`s'[_n-`num']

}  
}
}


*-------------------------------------------
* Statement Bias
*-------------------------------------------

foreach s in ard zdf rtl sat {
foreach partei in cdu spd fdp gru link {
egen finalton`partei'`s' = rowtotal(lg?ton`partei'`s'), missing
replace finalton`partei'`s' = finalton`partei'`s' /7
replace finalton`partei'`s' = . if date<date("05jul2009", "DMY")		
	//delete first seven days 
}
}

fre date
fsum finalton*		//92-7=85 observations


*-------------------------------------------------
* prepare for merging-process
*-----------------------------------------------

keep date finalton*

/* Create duplicate for each wave: Corresponding observations will be assigned 
with the "merge"-command. Also: add the prefix "a_" to all variables */

foreach var of varlist finalton*  {
foreach num of numlist 1/7 {
clonevar a_`var'`num' = `var'
}
}

foreach num of numlist 1/7 {
clonevar a_date`num' = date
}

keep a_*


save tvcontent2009.dta, replace

